<template>
	<om-formsub-outer :index="index" >
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, changeEditor} from '../../../static/js/formUtil.js';
	import {getDateStr} from '../../../static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				initValue:{
					files:[],//上次保存的文件列表
					busCode:''
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				formdata:{},
				orderNumCacheKey:'pm-work-report-ordernum-cache',
				reportCode:'DD021010002',
				reportDay:'',
			}
		},
		methods: {
			verifyActualEndDate(date){
				let _that = this
				var standDate = new Date();
				standDate.setDate(standDate.getDate() - _that.reportDay - 1);
				var d = new Date(date);
				if(_that.reportDay && d.getTime() < standDate.getTime()){
					return "管控参数配置中做了约束，“实际完成日期”只能选择当前日期、或当前日期之前 " + _that.reportDay + " 天内的日期，请重新编辑！";
				}
				return true
			},
			onSelectActualEndDate(date){
				this.om.request('tkeyWorkReport/verifyDate',{
					'date': date,
					'orderNum': this.formdata.orderNum,
					'itemName': this.formdata.itemName,
					'deliveryName': this.formdata.deliveryName
				}).then(res=>{
					if(res.succeed && res.value){
						_that.$refs.autoform.formdata.actualEndDate = ""
					}
				})
			}
		},
		onLoad(e){
			let _that = this
			_that.om.request('dataDict/getIntByCode',{code:_that.reportCode}).then(res=>{
				if(res.succeed){
					 _that.reportDay = res.value
				}
			})
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
								if(e.busCode){
									_that.initValue.busCode = e.busCode
								}
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor(_that.autoformdata[0].editors,"actualEndDate","max", getDateStr(new Date()))
								changeEditor(_that.autoformdata[0].editors,"orderNum","link","../../public/search/search")
								changeEditor(_that.autoformdata[0].editors, "orderNum", "linkParam", {
									other:JSON.stringify({
										sList: _that.datas.filter((e,i) => {
											return (i+1) != _that.order
										}).map(e=>e.orderNum + e.itemName + e.planEndDate).join(",")
									}),
									valuefield:"orderNum",
									namefield:"itemName",
									notefield:"workStandard",
									extrafield:"planEndDate",
									searchfield:"q",
									initload:true,
									url:"tkeyWorkReport/orderNumList2",
									cachename:_that.orderNumCacheKey
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileSaveNameField", "fileSaveName")
								changeEditor(_that.autoformdata[0].editors, "fileList", "uploadUrl", "uploadFileData/uploadInterimFile")
								changeEditor(_that.autoformdata[0].editors, "fileList", "loadUrl", "uploadFileData/getInterimFile")
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", 82)
								if(_that.formdata.fileList) {
									var tempFileList = []
									var fileUrlList = _that.formdata.fileList.map(t=>{
										tempFileList.push(t.fileSaveName)
										return t.fileSaveName
									})
									changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
									changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
								}
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.orderNumCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.listId = d.listId
							formdata.orderNum = d.orderNum
							formdata.itemName = d.itemName
							formdata.workStandard = d.workStandard
							formdata.deliveryName = d.deliveryName
							formdata.deliveryName = d.deliveryName
							formdata.keyItem = d.keyItem
							formdata.planEndDate = d.planEndDate
							formdata.evaluateUserName = d.evaluateUserName
							uni.removeStorage({
								key: _that.orderNumCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
